/*
 *            DO YOU WHAT THE WANT TO PUBLIC LICENSE
 *
 * Everyone is permitted to copy and distribute verbatim or modified
 * copies of this license document, and changing it is allowed as long
 * as the name is changed.
 *
 *           DO YOU WHAT THE WANT TO PUBLIC LICENSE
 *  TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
 *
 * 0. You just DO WHAT YOU WANT TO DO.
 */

package com.iurmfy.deskpet;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

/**
 * control frame to show the stage
 *
 * @author iurmfy
 * @version 1.0.0
 */
public class PetFrame extends JFrame
{
    /*
     * 窗体拖拽
     */
    private int frameX;
    private int frameY;
    private boolean isDrawing = false;

    // 宠物展示舞台
    private final PetStage stage;

    /**
     * create a new show frame
     *
     * @param petImage the image of a pet
     * @param multiple the pet size
     */
    public PetFrame(Image petImage, int multiple)
    {
        /*
         * 舞台
         */

       stage = new PetStage(petImage, multiple);

        // 场景大小与窗体一致
        setSize(stage.getSize());

        // 窗体剧中
        setLocationRelativeTo(null);

        // 保持一直在所有窗体顶部
        setAlwaysOnTop(true);

        // 解除窗体的边框
        setUndecorated(true);
        // 透明背景
        setBackground(new Color(0, 0, 0, 0));

        add(stage);

        /*
         * 添加拖事件
         */

        // 是否按下
        addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                // 按下窗体代表将要移动
                isDrawing = true;
                frameX = e.getX();
                frameY = e.getY();
            }
            public void mouseReleased(MouseEvent e)
            {
                isDrawing = false;
            }
        });
        addMouseMotionListener(new MouseMotionAdapter()
        {
            public void mouseDragged(MouseEvent e)
            {
                // 已经判断图片要移动
                if (isDrawing)
                {
                    int left = getX();
                    int top = getY();
                    // 原来的坐标加上移动的距离
                    setLocation(left + e.getX() - frameX, top + e.getY() - frameY);
                }
            }
        });
    }

    // 显示
    public void showPet()
    {
        setVisible(true);
    }
}
